<?php
date_default_timezone_set('Europe/Paris');
$basePath = realpath(__DIR__ . '/../');
$importAfter = array();

spl_autoload_register(
    function($name){
        global $basePath;
        $file = realpath($basePath.'/../'.str_replace('\\', '/', $name).'.class.php') ;
        if (isNullOrEmptyString($file) || !file_exists($file)) {
        	throw new Exception('Impossible to load class "'.$name.'" !');
        }
        require_once($file) ;
    }
);

function getClassName(/*string*/ $path)
{
    $actionName = $path ;
    $pointPos = strrpos($path, '.');
    if ($pointPos !== false) {
        $actionName = '\\PsiFramework\\' . str_replace('.', '\\', $path);
    }
    return $actionName ;
}

/**
 * Import a class
 *
 * @param $class    string    the class to import with its package information
 */
function import(/*string*/ $class)
{
    global $basePath, $importAfter ;
    $path = $basePath . '/' . str_replace('.', '/', $class) . '.class.php';
    $file = realpath($path) ;
    if (isNullOrEmptyString($file) || !file_exists($file)) {
        throw new Exception('Impossible to load class "'.$class.'" ('.$path.') !');
    }
    require_once($file);

    if (array_key_exists($file, $importAfter)) {
        foreach ($importAfter[$file] as $subFile) {
            require_once($subFile);
        }
        unset($importAfter[$file]);
    }
}

/**
 * Import a class after the class loading
 *
 * @param $class    string    the class to import with its package information
 */
function importAfter(/*string*/ $class)
{
    global $basePath, $importAfter ;

    $file = realpath(
        $basePath . '/' . str_replace('.', '/', $class) . '.class.php'
    );
    $trace  = debug_backtrace();
    $parentFile = realpath($trace[0]['file']);

    if (!array_key_exists($parentFile, $importAfter)) {
        $importAfter[$parentFile] = array();
    }
    $importAfter[$parentFile][] = $file ;
}

/**
 * Import a library from the Lib directory
 *
 * @param $lib    string    the library to load
 */
function importLibrary(/*string*/ $lib)
{
    global $basePath ;
    $lib = realpath($basePath . '/' . $lib);
    require_once($lib);
}

/**
 * Import a file
 *
 * @param $file    string    the file to load
 */
function importFile(/*string*/ $file)
{
    global $basePath ;
    $file = realpath($basePath . '/' . $file);
    require($file);
}

/**
 * Gets the www directory
 *
 * @return the path to the www directory
 */
function getWwwDirectory()
{
	global $basePath ;
	return realpath($basePath . '/../www');
}

/**
 * Gets the applications directory
 *
 * @return the path to the applications directory
 */
function getApplicationsDirectory()
{
    global $basePath ;
    return realpath($basePath . '/Applications');
}

/**
 * Gets the plugins directory
 *
 * @return the path to the plugins directory
 */
function getPluginsDirectory()
{
    global $basePath ;
    return realpath($basePath . '/Plugins');
}

/**
 * Gets the application directory
 *
 * @return the path to the application directory
 */
function getApplicationDirectory(/*string*/ $applicationName)
{
    return getApplicationsDirectory() . '/' . $applicationName;
}

/**
 * Gets the plugin directory
 *
 * @return the path to the plugin directory
 */
function getPluginDirectory(/*string*/ $pluginName)
{
    return getPluginsDirectory() . '/' . $pluginName;
}

/**
 * Gets the application config directory
 *
 * @return the path to the application config directory
 */
function getApplicationConfigDirectory(/*string*/ $applicationName)
{
    return getApplicationDirectory($applicationName) . '/Config';
}

/**
 * Gets the plugin config directory
 *
 * @return the path to the plugin config directory
 */
function getPluginConfigDirectory(/*string*/ $pluginName)
{
    return getPluginDirectory($pluginName) . '/Config';
}

/**
 * Gets the config directory
 *
 * @return the path to the config directory
 */
function getConfigDirectory()
{
    global $basePath ;
    return realpath($basePath . '/Config');
}

/**
 * Gets the config file passed by its name
 *
 * @param $config    string    the config to retrieve
 * @return the path to the config file
 */
function getConfigFile(/*string*/ $config)
{
    return getConfigDirectory() . '/' . $config;
}

function getPluginFilePath(/*string*/ $partialFilePath)
{
    $pluginNameEnd = strpos($partialFilePath, '}');
    $plugin = substr($partialFilePath, 1, $pluginNameEnd-1);
    $filePath = substr($partialFilePath, $pluginNameEnd+2);
    $file = getPluginDirectory($plugin) . '/' . $filePath ;
    return $file ;
}

function getApplicationFilePath(/*string*/ $partialFilePath)
{
    $applicationNameEnd = strpos($partialFilePath, ']');
    $application = substr($partialFilePath, 1, $applicationNameEnd-1);
    $filePath = substr($partialFilePath, $applicationNameEnd+2);
    $file = getApplicationDirectory($application) . '/' . $filePath ;
    return $file ;
}

function getFile(/*string*/ $partialFilePath)
{
    $file = null ;
    if (startsWith($partialFilePath, '{') == true) {
        $file = getPluginFilePath($partialFilePath);
    } else if (startsWith($partialFilePath, '[') == true) {
        $file = getApplicationFilePath($partialFilePath);
    }

    if (is_null($file) || !file_exists($file)) $file = null ;

    return $file;
}

function isNullOrEmptyString(/*string*/ $string)
{
    return (is_null($string) || $string === '');
}

function ckEditorFilter(/*string*/ $string)
{
    if (is_null($string)) {
        return null;
    }
    
    $string = str_replace(array("\r\n", "\n", "\r", "\t"), '', $string);
    $string = strip_tags($string, '<h1><h2><h3><h4><h5><h6><p><a><strong><u><em><ul><ol><li><img><br/><span>');
    
    return $string;
}

function startsWith(/*string*/ $haystack, /*string*/ $needle)
{
    $needleLength = strlen($needle);
    $haystackLength = strlen($haystack);
    if ($needleLength > $haystackLength) return false ;
    $stringStart = substr($haystack, 0, $needleLength);
    return $stringStart === $needle ;
}

function endsWith(/*string*/ $haystack, /*string*/ $needle)
{
    $needleLength = strlen($needle);
    $haystackLength = strlen($haystack);
    if ($needleLength > $haystackLength) return false ;
    $stringEnd = substr(
        $haystack,
        $haystackLength-$needleLength,
        $needleLength
    );
    return $stringEnd === $needle ;
}
